//
//  Ch3_4Controller.swift
//  learn-gl-es-swift
//
//  Created by LimitLiu on 2019/9/11.
//  Copyright © 2019 LimitLiu. All rights reserved.
//

import UIKit
import GLKit

struct SceneVertex3_4 {
    var positionCoords: GLKVector3
    var textureCoords: GLKVector2
}

class Ch3_4Controller: GLKViewController {
    
    var baseEffect = GLKBaseEffect()
    var vertexBuffer: GLVertexAttribArrayBuffer!
    var textureInfo0: GLKTextureInfo!
    var textureInfo1: GLKTextureInfo!
    
    var vertices: [SceneVertex3_4] = [
        SceneVertex3_4(positionCoords: vec(-1, -0.67, 0), textureCoords: vec(0, 0)),
        SceneVertex3_4(positionCoords: vec(1, -0.67, 0), textureCoords: vec(1, 0)),
        SceneVertex3_4(positionCoords: vec(-1, 0.67, 0), textureCoords: vec(0, 1)),
        
        SceneVertex3_4(positionCoords: vec(1, -0.67, 0), textureCoords: vec(1, 0)),
        SceneVertex3_4(positionCoords: vec(-1, 0.67, 0), textureCoords: vec(0, 1)),
        SceneVertex3_4(positionCoords: vec(1, 0.67, 0), textureCoords: vec(1, 1)),
    ]
    
    deinit {
        let view = self.view as! GLKView
        GLContext.setCurrent(view.context)
        vertexBuffer = nil
        GLContext.setCurrent(nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        let view = self.view as! GLKView
        view.context = GLContext(api: .openGLES2)!
        GLContext.setCurrent(view.context)
        baseEffect.useConstantColor = GLboolean(GL_TRUE)
        baseEffect.constantColor = vec(1, 1, 1, 1)
        (view.context as! GLContext).clearColor = vec(0, 0, 0, 1)
        vertexBuffer = GLVertexAttribArrayBuffer(
            attribStride: GLsizei(MemoryLayout<SceneVertex3_4>.stride),
            numberOfVertices: GLsizei(vertices.count),
            data: vertices,
            usage: GLenum(GL_STATIC_DRAW)
        )
        let imageRef0 = UIImage(named: "leaves")!.cgImage!
        let imageRef1 = UIImage(named: "beetle")!.cgImage!
        textureInfo0 = try! GLKTextureLoader.texture(
            with: imageRef0,
            options: [GLKTextureLoaderOriginBottomLeft : true as NSNumber]
        )
        textureInfo1 = try! GLKTextureLoader.texture(
            with: imageRef1,
            options: [GLKTextureLoaderOriginBottomLeft : true as NSNumber]
        )
        glEnable(GLenum(GL_BLEND))
        glBlendFunc(GLenum(GL_SRC_ALPHA), GLenum(GL_ONE_MINUS_SRC_ALPHA))
    }
    
    override func glkView(_ view: GLKView, drawIn rect: CGRect) {
        (view.context as! GLContext).clear(GLbitfield(GL_COLOR_BUFFER_BIT))
        vertexBuffer.prepareToDraw(
            attrib: GLuint(GLKVertexAttrib.position.rawValue),
            numberOfCoordinates: 3,
            attribOffset: GLsizeiptr(0),
            shouldEnable: true
        )
        vertexBuffer.prepareToDraw(
            attrib: GLuint(GLKVertexAttrib.texCoord0.rawValue),
            numberOfCoordinates: 2,
            attribOffset: GLsizeiptr(MemoryLayout<GLfloat>.size * 4),
            shouldEnable: true
        )
        baseEffect.texture2d0.name = textureInfo0.name
        baseEffect.texture2d0.target = GLKTextureTarget(rawValue: textureInfo0.target)!
        baseEffect.prepareToDraw()
        vertexBuffer.drawArray(
            mode: GLenum(GL_TRIANGLES),
            startVertexIdx: 0,
            numberOfVertices: GLsizei(vertices.count)
        )
        
        baseEffect.texture2d0.name = textureInfo1.name
        baseEffect.texture2d0.target = GLKTextureTarget(rawValue: textureInfo1.target)!
        baseEffect.prepareToDraw()
        vertexBuffer.drawArray(
            mode: GLenum(GL_TRIANGLES),
            startVertexIdx: 0,
            numberOfVertices: GLsizei(vertices.count)
        )
        
    }

}
